<?php

namespace app\api\controller;

use think\Db;
use app\common\validate\GoodsValidate;

class Index extends ApiBase
{
	protected $datas;

	/**
	 * 首页数据
	 * @Author   Yichen
	 * @DateTime 2020-05-02T14:56:39+0800
	 * @return   [type]                   [description]
	 */
	public function homeData()
	{
		$bannerList = Db::name('t_banner')
			->where('status', 1)
			->field('id,name,image,url')
			->select();
		$goodsList = Db::name('t_goods')
			->where('status', 1)
			->field('id,name,price,discount_price discountPrice,cover_img coverImg,description')
			->select();
		return json([
			'code' => 1, 
			'msg' => '', 
			'data' => [
				'bannerList' => $bannerList,
				'goodsList' => $goodsList
			]
		]);	
	}	


	/**
	 * 搜索
	 * @Author   Yichen
	 * @DateTime 2020-05-04T20:47:53+0800
	 * @return   [type]                   [description]
	 */
	public function search()
	{
		$this->datas = $this->params;
		$validate = new GoodsValidate();
		if (!$validate->scene('indexSearch')->check($this->datas)) {
			return json(['code' => 0, 'msg' => $validate->getError(), 'data' => '']);
		}
		$keyword = $this->datas['keyword'];
		$data = Db::name('t_goods')
			->whereLike('name', '%' . $keyword . '%')
			->where('status', 1)
			->field('id,name,price,discount_price discountPrice,cover_img coverImg,description')
			->select();
		if (empty($data)) {
			return json(['code' => 0, 'msg' => '宝贝不存在', 'data' => []]);
		}	
		return json(['code' => 1, 'msg' => '', 'data' => $data]);	
	}
}